import axios from 'axios'
import {getToken} from '@/utils/auth'
import Config from '@/settings'
import qs from 'qs'

// 创建axios实例
const service = axios.create({
    baseURL: Config.baseURL, // api 的 base_url
    timeout: Config.timeout // 请求超时时间
});

// request拦截器
service.interceptors.request.use(
	config => {
		if (getToken()) {
			config.headers['Authorization'] = getToken() //让每个请求携带自定义token
        }
		config.headers['Content-Type'] = Config.contentType;
        //对参数进行qs的序列化
		config.data = qs.stringify(config.data);
		return config;
    },
    error => {
        // 对请求错误做些什么，自己定义
		//...
		return new Promise(function(resolve, reject) {
			reject(error);
		}).catch(function(reason) {
			console.log('catch:', reason);
		});
    }
);

// response 拦截器
service.interceptors.response.use(
	response => {
		//项目级别的状态码处理，0代表正常
		if (response.data.errorNo === 0) {
			return Promise.resolve(response);
		} else {
			//...
			return new Promise(function(resolve, reject) {
				reject(response);
			}).catch(function(reason) {
				console.log('catch:', reason);
			});
		}
	},
    error => {
		let code = 0;
		//网络请求超时
		try {
			code = error.response.status;
		} catch (e) {
			if (error.toString().indexOf('Error: timeout') !== -1) {
				//...
				return new Promise(function(resolve, reject) {
					reject(error);
				}).catch(function(reason) {
					console.log('catch:', reason);
				});
			}
		}
		//其它有状态码的返回情况
		if (code) {
			//...
			return new Promise(function(resolve, reject) {
				reject(error);
			}).catch(function(reason) {
				console.log('catch:', reason);
			});
		}
		//网络断开情况
		if(error.toString().indexOf('Error: Network Error') !== -1) {
			//...
			return new Promise(function(resolve, reject) {
				reject(error);
			}).catch(function(reason) {
				console.log('catch:', reason);
			});
		}
		//其他情况
		//...
		return new Promise(function(resolve, reject) {
			reject(error);
		}).catch(function(reason) {
			console.log('catch:', reason);
		});
    }
);
export default service